<?php
require_once("config/config.php");
$yahooapi_Query=rawurlencode(trim(_GET("q")));
//$yahooapi_Query = str_replace(array("++","+++","++++"),"",$yahooapi_Query);
if(empty($yahooapi_Query)||$yahooapi_Query=="+"){exit();}
else{
  $yahooapi_url="http://suggest.search.yahooapis.jp/SuggestSearchService/V3/webassistSearch";
  $yahoosearch="http://search.yahooapis.com/WebSearchService/V1/relatedSuggestion";
  $yahooapi_appid="CdTXPgixg64BiwGOjQqxTkoYoxXkdFskzTqgcvzlKazkkXgZleBkQKJowsx_6pBoEsn6";
  $yahooapi_output="json";
  $yahooapi_src1="auc";
  $yahooapi_src2="shp";

  $yahooapi_page1=$yahooapi_url."?appid=".$yahooapi_appid."&output=".$yahooapi_output."&Query=".$yahooapi_Query."&.src=".$yahooapi_src1;
  $yahooapi_page2=$yahooapi_url."?appid=".$yahooapi_appid."&output=".$yahooapi_output."&Query=".$yahooapi_Query."&.src=".$yahooapi_src2;
  $yahooapi_appid="fSshV3fV34Fd2xh65VV8xKrVH2trjKPL3BaiMHAQCnpOIai7wO9UhWisSEb8qAbRPA--";
  $yahooapi_page3=$yahoosearch."?appid=".$yahooapi_appid."&output=".$yahooapi_output."&query=".$yahooapi_Query;
  
  
  $suggest=array();
  $curl = new curl();
  try{
		$curl->_url = $yahooapi_page3;
		$yahoosearch_json = $curl->execute();
		$yahoosearch_data = json_decode($yahoosearch_json);
		if(is_array($yahoosearch_data)&&!empty($yahoosearch_data)){
        foreach($yahoosearch_data as $value )
        {
          if(is_array($value->Result)){
            foreach($value->Result as $val)
            {
               $suggest[$val]=$val;
            }
          }
        }
    }
  }catch(Exception $e) { 
  }
  //$localdata=file_get_contents("https://www.fromjapan.co.jp/Search/index.php/Index/ajaxGetKeyword.html?q=".$yahooapi_Query."&amp;limit=".$_GET["limit"]."&amp;timestamp=".$_GET["timestamp"]);
  //$localdata=file_get_contents("http://fj.kansea.com/Search/index.php/Index/ajaxGetKeyword/q/".$yahooapi_Query."/limit/".$_GET["limit"]."/timestamp/".$_GET["timestamp"]."/");
  //$localdata=file_get_contents("https://www.fromjapan.co.jp/Search/index.php/Index/ajaxGetKeyword/q/".$yahooapi_Query."/limit/".$_GET["limit"]."/timestamp/".$_GET["timestamp"]."/");
  //echo $localdata;
  //echo "-----------------\n";
  
  $suggest=suggestfix($curl,$suggest,$yahooapi_page1);
  $suggest=suggestfix($curl,$suggest,$yahooapi_page2);
  //echo json_encode($suggest);
  echoarray($suggest);
}
function suggestfix($curl,$array,$page_url){
	$curl->_url = $page_url;
	$yahooapidata=$curl->execute();
	$yahooapi_suggestapi_data = json_decode($yahooapidata) ;
	if(is_array($yahooapi_suggestapi_data)&&!empty($yahooapi_suggestapi_data)){
      foreach($yahooapi_suggestapi_data as $Key=>$val){
        if(is_array($val)){
          foreach($val as $KeyA=>$valA){
            $array[$valA]=NULL;
          }
        }else{
          $array[$val]=NULL;
        }
      }
	}
	return $array;
}
function echoarray($array){
  if(is_array($array)&&!empty($array)){
      foreach($array as $key=>$val){
        echo $key."\n";
      }
	}
}